require("lsqlite3")
require("xml")

ADD_OPTIONAL_PATTERNS = true

ITEM_SPELL_TRIGGER_ONEQUIP = "Equip:"
WEAPON_SPEED = "Speed"
EMPTY_SOCKET_RED = "Red socket"
EMPTY_SOCKET_BLUE = "Blue socket"
EMPTY_SOCKET_YELLOW = "Yellow socket"
EMPTY_SOCKET_META = "Meta socket"

local LANG = (arg[1] or "EN"):upper()

local LOCALE
if LANG == "EN" or LANG == "US" then
	LOCALE = "enUS"
elseif LANG == "FR" then
	LOCALE = "frFR"
else
	error("Unknown locale: "..LANG)
end

local output, force_debug

AceLibrary = setmetatable({
	libs = {
		["AceAddon-2.0"] = { 
			new = function () 
				return {
					debug = true,
					Debug = function (self, ...) if force_debug or self.debug then output(string.format(...)) end end,
					SetDebugging = function (self, v) self.debug = v end,
				}
			end
		}
	},
	IsNewVersion = function () return true end,
	HasInstance = function () return true end,
	Register = function (self, lib, name) self.libs[name] = lib end,
	_get = function (self, name)
		return self.libs[name]
	end,
}, { __call = function (self, ...) return self:_get(...) end })

GetLocale = function () return LOCALE end

local loadfile = function (file)
	local f, err = io.open(file, "r")
	if not f then
		return nil, err
	end
	local first = true
	return load(function ()
			if first then
				first = false
				local line = f:read()
				if line:sub(1, 3) == "\239\187\191" then
					return line:sub(4).."\n"
				else
					return line.."\n"
				end
			else
				local line = f:read()
				if line then
					return line.."\n"
				end
			end
		end, file)
end

local status, result = loadfile(os.getenv("HOME").."/dev/temp/dev/ItemBonusLib/ItemBonusLib-1.0/ItemBonusLib-1.0.lua")

if not status then
	print("ERROR loading ItemBonusLib", result)
	return
end

status()

function test_cache()
	local db = sqlite3.open("armory.db")
	local ibl = AceLibrary("ItemBonusLib-1.0")
	
	local item_id
	local string_format = string.format
	local out = io.open("ibl-report-"..LANG:upper()..".txt", "w")
	force_debug = true
	local called
	local e_cache = {}
	output = function (s)
		called = true
		if not e_cache[s] then
			out:write(string_format("%d : %s\n", item_id, s))
			e_cache[s] = true
		end
	end

	local sets = {}
	local count, erroneous = 0, 0
	local time = 0
	local clock = os.clock
	for data in db:urows("SELECT content FROM cache WHERE lang = '"..LANG.."' AND type = 'tooltip'") do
		local tooltip = xml.parse(data):get_first_child("itemTooltips"):get_first_child("itemTooltip")
		if tooltip then
			local spells = tooltip:get_first_child("spellData")
			if spells then
				local index = 1
				item_id = tonumber(tooltip:get_first_child("id"):get_content())
				for spell in spells:iterate_children("spell") do
					local trigger = tonumber(spell:get_first_child("trigger"):get_content())
					if trigger == 1 then
						local bonus = spell:get_first_child("desc"):get_content()
						called = nil
						count = count + 1
						local s = clock()
						ibl:AddBonusInfo({}, bonus)
						time = time + (clock() - s)
						if called then
							erroneous = erroneous + 1
						end
					end
				end
			end
			local info = tooltip:get_first_child("setData")
			if info then
				item_id = tonumber(tooltip:get_first_child("id"):get_content())
				local name = info:get_first_child("name"):get_content()
				if not sets[name] then
					sets[name] = true
					for child in info:iterate_children("setBonus") do
						local bonus = child:get_attribute("desc")
						called = nil
						count = count + 1
						ibl:AddBonusInfo({}, bonus)
						if called then
							erroneous = erroneous + 1
						end
					end
				end
			end
		end
	end
	out:close()
	print(string_format("%d out of %d bonus failed", erroneous, count), time)
end

test_cache()
